package com.util.net;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ByteArrayOutputStream;
import java.util.Properties;
import java.net.*;
/**
 * @author  <a href="mailto: zhuyoulong@gmail.com">zhuyoulong</a>
 * @create_date 2008-10-31
 * @version $ Revision: 1.0
 */
public class GetRemoteHtml {

    public GetRemoteHtml() {
    }

    public static String getforwordsUrl(String url) {
        //  String linkurl = "";
        //int pos1,pos2,pos3,temp = -1;
        String st = "";
        try {

            URL rooturl = new URL(url);
            InputStream inputstream = rooturl.openStream();

            ByteArrayOutputStream outputstream = new ByteArrayOutputStream();
            //StringBuffer str = new StringBuffer();
            byte[] bytes = new byte[1024];
            int i = -1;
            while ((i = inputstream.read(bytes)) != -1) {
                outputstream.write(bytes, 0, i);
            }
            st = outputstream.toString();
            //   System.out.println(st);


        } catch (Exception e) {
            System.out.println("e:" + e.toString());
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }


        return st;

    }


    public static String getRemoteFile(String urlStr, String proxyHost, String proxyPort) {
        String sCurrentLine = "";
        StringBuffer sb = new StringBuffer();
        try {
            if (proxyHost != null && proxyPort != null) {
                Properties prop = System.getProperties();
                prop.setProperty("http.proxyHost", proxyHost);
                prop.setProperty("http.proxyPort", proxyPort);
            }
            URL url = new URL(urlStr);
            URLConnection connection = url.openConnection();

            String type = connection.getHeaderField("Content-Type");
            String charsert;
            if (type != null && type.indexOf("charset=") > -1) {
                charsert = type.substring(type.indexOf("charset=") + "charset=".length());
            } else {
                charsert = "gbk";
            }
            InputStream l_urlStream = connection.getInputStream();
            BufferedReader l_reader = new BufferedReader(new InputStreamReader(l_urlStream,charsert));
            while ((sCurrentLine = l_reader.readLine()) != null) {
                sb.append(sCurrentLine);
            }
            try {
                l_reader.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return sb.toString();
    }

    public static String getRemoteFile(String url) {
        java.lang.String sCurrentLine = "";
        StringBuffer sb = new StringBuffer();
        try {
            java.net.URL l_url = new URL(url);
            java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url.openConnection();
            l_connection.connect();

            String type = l_connection.getHeaderField("Content-Type");
            String charsert;
            if (type != null && type.indexOf("charset=") > -1) {
                charsert = type.substring(type.indexOf("charset=") + "charset=".length());
            } else {
                charsert = "gbk";
            }
            InputStream l_urlStream = l_connection.getInputStream();
            BufferedReader l_reader = new BufferedReader(new InputStreamReader(l_urlStream, charsert));
            while ((sCurrentLine = l_reader.readLine()) != null) {
                sb.append(sCurrentLine);
            }
            try {
                l_connection.disconnect();
            } catch (java.lang.Exception ex) {
                //ex.printStackTrace();
            }
            try {
                l_urlStream.close();
            } catch (java.lang.Exception ex) {
                //ex.printStackTrace();
            }

            try {
                l_reader.close();
            } catch (java.lang.Exception ex) {
                //ex.printStackTrace();
            }
        } catch (java.lang.Exception ex) {
            //ex.printStackTrace();
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        //String reford = getRemoteFile("http://www.mov6.com/title/tt0318761/", "219.149.233.179", "8080");
        String reford = getRemoteFile("http://www.allmov.com/movie/10000/main.html");
        System.out.println("sss:" + reford);
    }
}
